import logging.handlers
import os

from config import BASE_PATH


class GetLog:
    logger = None

    # 获取日志器的方法
    @classmethod
    def get_logger(cls):
        # 判断日志器如果为空
        if cls.logger is None:
            # 日志路径
            log_path = BASE_PATH + os.sep + "log" + os.sep + "ihrm.log"
            # 获取日志器
            cls.logger = logging.getLogger()
            # 设置日志级别
            cls.logger.setLevel(logging.INFO)
            # 获取日志器的处理器
            th = logging.handlers.TimedRotatingFileHandler(log_path, when="h", interval=1, backupCount=3,
                                                           encoding="utf-8")
            # 设置日志格式器
            fmt = "%(asctime)s %(levelname)s [%(filename)s %(funcName)s:%(lineno)d] - %(message)s"
            formatter = logging.Formatter(fmt)
            # 给处理器添加格式器
            th.setFormatter(formatter)
            # 给日志器添加处理器
            cls.logger.addHandler(th)

        return cls.logger
